package com.ky.pro.big.data.mr.wc;

import com.ky.pro.big.data.mr.common.EnvConsts;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;

public class WordCountTool extends Configured implements Tool {


    public static final String SYS_PROP_INPUT_PATH = "ky.mr.wc.path.in";
    public static final String SYS_PROP_OUTPUT_PATH = "ky.mr.wc.path.out";


    @Override
    public int run(String[] strings) throws Exception {
        Configuration conf = super.getConf();

        Job job = Job.getInstance(conf, "wordCountDemo");

        job.setJarByClass(WordCountTool.class);

        job.setInputFormatClass(TextInputFormat.class);
        TextInputFormat.addInputPath(job, new Path(System.getProperty(
                EnvConsts.SYS_PROP_WORD_COUNT_INPUT_PATH)));

        job.setMapperClass(WordCountMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setReducerClass(WordCountReducer.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        TextOutputFormat.setOutputPath(job, new Path(System.getProperty(
                EnvConsts.SYS_PROP_WORD_COUNT_OUTPUT_PATH)));

        boolean b = job.waitForCompletion(true);
        return b ? 0 : 1;

    }

}
